package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhouwei
 * @date 2020-01-17 9:28
 */

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;
    @Autowired
    private MenuDao menuDao;


    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        //调用分页助手
        PageHelper.startPage(currentPage,pageSize);//设置当前页码，每页条数
        Page<Role> page=roleDao.findPage(queryPageBean);//条件查询
        long total = page.getTotal();//总页码
        List<Role> result = page.getResult();//所选页信息
        PageResult pageResult=new PageResult(total,result);
        return pageResult;
    }


    /**
     * 查询菜单
     * @return
     */
    @Override
    public List<Menu> findMenu() {
        List<Menu> menuList = roleDao.findMenu();
        return menuList;
    }

    /**
     * 添加角色
     * @param role
     * @param menuIds
     */
    @Override
    public void add(Role role, Integer[] menuIds) {
        //添加角色到role表
        roleDao.addRole(role);
        Integer roleId = role.getId();
        //添加菜单id到关联表
        for(Integer i:menuIds){
            Map<String,Integer> map=new HashMap<>();
            map.put("roleId",roleId);
            map.put("menuId",i);
            roleDao.addRoleMenu(map);
        }
    }

    /**
     * 查询单个role所有信息
     * @param id
     * @return
     */
    @Override
    public Map findById(Integer id) {
        //查询role基本信息
        Role role = roleDao.findById(id);
        //查询已选菜单信息
        List<Integer> menuids = menuDao.findByRoleid(id);
        //查询所有菜单
        List<Menu> list = roleDao.findMenu();
        //封装到map
        Map<String,Object> data=new HashMap<>();
        data.put("role",role);
        data.put("menuids",menuids);
        data.put("list",list);
        return data;
    }

    /**
     * 编辑role
     * @param role
     * @param menuIds
     */
    @Override
    public void edit(Role role, Integer[] menuIds) {
        //先改关联表，先删掉所有关联表，在添加新的关联关系
        for(Integer i:menuIds){
            Map<String,Integer> map=new HashMap<>();
            map.put("menuId",i);
            map.put("roleId",role.getId());
            roleDao.deleteRoleMenu(role.getId());
            roleDao.addRoleMenu(map);
        }
        //再改role
        roleDao.editRole(role);
    }



}
